﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace QUT.Bio.Util {
	/// <summary>
	/// A hyperlink-looking button that opens a web page in the default browser. In Silverlight,
	/// use HyperlinkButton. In WPF we implement a basic custom control. When clicked it will try to open
	/// a new browser window to display specified content.
	/// </summary>

#if SILVERLIGHT
	public class BrowserLinkButton : System.Windows.Controls.HyperlinkButton {
		#region Constructor
		/// <summary>
		/// Sets the TargetName property of base class.
		/// </summary>

		public BrowserLinkButton () {
			base.TargetName = "NewBrowserWindow";
		} 
		#endregion

		#region Property: Address
		/// <summary>
		/// Get or set the address to which this button will navigate.
		/// </summary>

		public Uri Address {
			get {
				return base.NavigateUri;
			}
			set {
				base.NavigateUri = value;
			}
		} 
		#endregion
		
		#region Property: ButtonContent
		/// <summary>
		/// Get or set the content of this hyperlink button.
		/// </summary>
		
		public string ButtonContent {
			get {
				return base.Content as string;
			}
			set {
				base.Content = value;
			}
		}
		#endregion
	}
#else
	public class BrowserLinkButton: LinkButton {
		private Uri uri;
		
		/// <summary>
		/// Initialise a new BrowserLinkButton.
		/// </summary>
		
		public BrowserLinkButton () {
			Click += ButtonClicked;
		}
		
		/// <summary>
		/// Get or set the address of the page that will be opened when this button is clicked.
		/// </summary>
		
		public Uri Address {
			get {
				return uri;
			}
			set {
				uri = value;
			}
		}
		
		/// <summary>
		/// Opens the web page.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		
		private void ButtonClicked( object sender, RoutedEventArgs e ) {
			if ( uri != null ) {
#if SILVERLIGHT
					System.Windows.Browser.HtmlPage.Window.Navigate( uri, "NewBrowserWindow" );
#else
					System.Diagnostics.Process.Start( uri.ToString() );
#endif
			}
		}
	}
#endif

}
